package com.jasonchoi.redisqueue;

import com.jasonchoi.redisqueue.pubsub.pub.CustomRedisSender;
import com.jasonchoi.redisqueue.queue.RedisQueueUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

import static com.jasonchoi.redisqueue.TestRedisConsumer.TEST_TOPIC;

/**
 * @Author: JasonChoi
 * @Date: 2020/3/20 16:22
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private RedisQueueUtil redisQueueUtil;


    @Autowired
    private CustomRedisSender customRedisSender;

    /**
     * 测试异步队列
     * @param req
     * @return
     */
    @RequestMapping(value = "/queue", method = RequestMethod.GET)
    @ResponseBody
    public String queue(String req) {
        redisQueueUtil.publish(req, TEST_TOPIC);
        return "queue send";
    }
    /**
     * 测试延时队列
     * @param req
     * @return
     */
    @RequestMapping(value = "/delay", method = RequestMethod.GET)
    @ResponseBody
    public String delay(String req, int delay) {
        redisQueueUtil.publish(req, delay, TimeUnit.SECONDS, TEST_TOPIC);
        return "delay send";
    }

    /**
     * 测试发布订阅模式(不支持持久化 ， 消息丢失问题)
     * @param req
     * @return
     */
    @RequestMapping(value = "/sub", method = RequestMethod.GET)
    @ResponseBody
    public String sub(String req) {
        customRedisSender.sendMessage(req);
        return "sub send";
    }
}
